<template>
  <div class="p9-container">
    <div class="chart-container" :style="{width:width}">
      <h2>Delivery Outcomes of {{currentCampainCategory}} Campaigns Launched in Past 5 Years</h2>

      <div :class="className" :id="id" :style="{height:height,width:width}"></div>
    </div>
  </div>
</template>

<script>
import echarts from "echarts";
import { feature9Data } from "@/api/feature";
import { mapState, mapMutations } from "vuex";

export default {
  props: {
    className: {
      type: String,
      default: "chart"
    },
    id: {
      type: String,
      default: "chart"
    },
    width: {
      type: String,
      default: "200px"
    },
    height: {
      type: String,
      default: "200px"
    }
  },
  data() {
    return {
      chart: null,
      featureData: {}
    };
  },
  mounted() {
    // this.initChart();
      if(this.canDraw) {
          this.initChart();
      }
  },
  beforeDestroy() {
    if (!this.chart) {
      return;
    }
    this.chart.dispose();
    this.chart = null;
  },
  watch: {
    canDraw(val) {
      if (val) {
        this.initChart();
      }
    }
  },
  computed: {
    ...mapState({
      currentCampainId: state => state.campaign.currentCampainId,
      currentCampainCategory: state => state.campaign.currentCampainCategory,
      canDraw: state => state.campaign.canDraw
    })
  },
  methods: {
    async initChart() {
      this.chart = echarts.init(document.getElementById(this.id));

      var legendData = [
        "Rewards Undelivered",
        "Undesired Rewards Delivered",
        "Delyed Rewards Delivered",
        "Satisfactory Rewards Delivered"
      ];

      var featureServerData = await feature9Data();

      console.log(featureServerData[this.currentCampainId]);

      var showData = featureServerData[this.currentCampainCategory];

      Object.keys(showData).forEach((item, key) => {
        Object.keys(showData[item]).forEach((categoryItem, categoryKey) => {
          if (this.featureData[categoryItem]) {
            this.featureData[categoryItem].push(showData[item][categoryItem]);
          } else {
            this.featureData[categoryItem] = [];
            this.featureData[categoryItem].push(showData[item][categoryItem]);
          }
        });
      });

      var finalData = [];

      Object.keys(this.featureData).forEach((item, key) => {
        finalData.push({
          name: item,
          type: "bar",
          barGap: "10%",
          data: this.featureData[item]
        });
      });

      //   featureServerData.forEach(item => {

      //   });

      let option = {
        color: ["#bf3030", "#d55442 ", "#e4954d", "#40b33f"],
        title: {
          // text: '某地区蒸发量和降水量',
          // subtext: '纯属虚构'
        },
        tooltip: {
          trigger: "axis"
        },
        legend: {
          data: [
            "Rewards Undelivered",
            "Undesired Rewards Delivered",
            "Delyed Rewards Delivered",
            "Satisfactory Rewards Delivered"
          ]
        },
        // toolbox: {
        //   show: true,
        //   feature: {
        //     mark: { show: true },
        //     dataView: { show: true, readOnly: false },
        //     magicType: { show: true, type: ["line", "bar"] },
        //     restore: { show: true },
        //     saveAsImage: { show: true }
        //   }
        // },
        calculable: true,
        xAxis: [
          {
            type: "category",
            name: "year",
            nameRotate: 45,
            nameGap: 5,
            data: ["2015", "2016", "2017", "2018", "2019"]
          }
        ],
        yAxis: [
          {
            type: "value",
            name: "Number of Campaigns"
          }
        ],
        series: finalData
      };

      this.chart.setOption(option);
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
.p9-container {
  width: 100%;
  height: 100%;
  h2 {
    text-align: center;
    margin-bottom: 26px;
  }
}
</style>

